package com.etc.admin.controller;

import java.awt.color.CMMException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.etc.admin.service.OrderService;
import com.etc.admin.service.OrderViewService;
import com.etc.admin.service.RoomService;
import com.etc.entity.Order;
import com.etc.entity.OrderView;
import com.etc.entity.Room;
import com.etc.util.CommonMessage;
import com.etc.util.CountMessage;
import com.etc.util.PageData;

/**
 * 
 * @author 蒋丽娟
 *
 */
@Controller
public class OrderController {
	@Resource(name = "OrderService")
	private OrderService os;
	@Resource(name = "OrderViewService")
	private OrderViewService ovs;
	@Resource(name = "roomService")
	private RoomService rs;
	@RequestMapping(value = "orderPage")
	@ResponseBody
	public PageData<OrderView> orderByPage(Integer page, Integer limit, String content) {
		if (content == null)
			content = "";
		PageData<OrderView> pd = ovs.getOrderByPage(page, limit, content);
		pd.setCode(0);
		pd.setMsg("ok");
		return pd;
	}

	@RequestMapping(value = "editOrder")
	@ResponseBody
	public CommonMessage editRoom(OrderView orderView) {
		CommonMessage cm = new CommonMessage();
		int roomno=orderView.getRoomno();//得到订单对应的房间号
		int orderid = orderView.getOrderid();//得到订单的id
	     Order order=os.getOrderById(orderid);//通过id得到订单的信息
	     int beforeorderstatus=order.getOrderstatus();//得到这个订单之前的状态
		int orderstatus = orderView.getOrderstatus();// 得到修改后的订单状态
		String orderstartdate = orderView.getOrderstartdate();// 得到订单的入住日期
		String orderenedate=orderView.getOrderenedate();// 得到订单的退房日期
		Date date = new Date();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		String nowDate = format.format(date);// 得到当前的时间
		if (orderstatus == 1 && orderstartdate.compareTo(nowDate) > 0) {// 如果修改为入住状态，要判断时间是否为当前时间//入住的时间大于当前的时间，就不能办理入住的状态
			cm.setMsg("入住时间还没到，不能修改为入住状态");
			return cm;
		}
		if(orderstatus==0 && beforeorderstatus!=1 ) {//如果修改为退房状态，要看之前是否已经入住
			cm.setMsg("本就不是入住状态，无法修改为退房状态");
			return cm;
		}
		if(orderenedate.compareTo(nowDate)<0) {
			cm.setMsg("时间已经过去，修改无效");
			return cm;
		}
		
		if(orderstartdate.compareTo(nowDate)<=0 && orderenedate.compareTo(nowDate)>=0) {//判断修改的订单的时间段是否包含了当天
			if(orderstatus==3) {
				orderstatus=0;
			}
			rs.upStatusByRoomno(roomno,orderstatus);//修改房间的状态
		}
		boolean flag=os.updateOrderById(orderid, orderstatus);
		
		if(flag) {
			cm.setMsg("修改成功");
		}else {
			cm.setMsg("修改失败");
		}
		return cm;
	}
	
	@RequestMapping(value = "getOrderCount",method=RequestMethod.GET)
	@ResponseBody
	public CountMessage getOrderCount() {
		CountMessage cm = new CountMessage();
		int orderCount = os.orderCount();
		int totalTurnover = os.totalTurnover();	
		cm.setTotalTurnover(totalTurnover);
		cm.setOrderCount(orderCount);
		return cm;
	}
	
}
